// Copyright 2008 web2me.dev
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package net.web2me.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.text.DefaultCaret;

import net.web2me.api.CommonConstants;
import net.web2me.api.Parsable;
import net.web2me.api.ParserFactory;

/**
 * A GUI client implemented with Swing
 * 
 * @author web2me
 *
 */
public class Web2MeGuiSwing {
	
	private static final int COMMON_HEIGHT = 25;
	/**
	 * 
	 */
	private static final long serialVersionUID = 3797626990480117629L;
	
	public static final int URL_FIELD = 2;
	
	public static final int DIR_FIELD = 3;
	
	public static final int PARSER_COMBO = 5;
	
	public static final int HISTORY_CHECK = 6;
	
	public static final int METADATA_CHECK = 7;
	
	public static final int START_BUTTON = 8;
	
	public static final int INFO_LABEL = 10;
	
	private static JTextArea loggerField = new JTextArea();
	
	public static JTextArea getLoggerTextField() {
		return loggerField;
	}

	public static void main(String[] args) throws IOException {
		JFrame frame = new JFrame("Web2Me - GUI client (Swing)");
		frame.setResizable(false);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setSize(380, 420);
		frame.setLocation(500, 500);
		
		JPanel panel = (JPanel) frame.getContentPane();
		
		JLabel urlLabel = new JLabel(GuiConstants.URL_LABEL_TEXT);
		urlLabel.setSize(350, 30);
		urlLabel.setBounds(10, 10, 350, COMMON_HEIGHT);
		
		JLabel dirLabel = new JLabel("Directory");
		dirLabel.setSize(350, 30);
		dirLabel.setBounds(10, 60, 350, COMMON_HEIGHT);
		
		JLabel parserLabel = new JLabel(GuiConstants.PARSER_LABEL_TEXT);
		parserLabel.setSize(350, 30);
		parserLabel.setBounds(10, 110, 350, COMMON_HEIGHT);
		
		JTextField urlField = new JTextField();
		urlField.setBounds(10, 35, 350, COMMON_HEIGHT);
		urlField.setText(CommonConstants.HTTP_DOT_DOT_SLASH_SLASH);
		
		JTextField dirField = new JTextField();
		dirField.setBounds(10, 85, 350, COMMON_HEIGHT);
		dirField.setText(System.getProperty("user.home"));
		
		Parsable[] parsers = ParserFactory.getAllParsers();
		
		JComboBox parsersCombo = new JComboBox(parsers);
		parsersCombo.setBounds(10, 135, 350, COMMON_HEIGHT);
		parsersCombo.addActionListener(new ParsersComboActionListener(panel));
		
		JCheckBox historyCheck = new JCheckBox(GuiConstants.HISTORY_CHECK_TEXT);
		historyCheck.setBounds(10, 180, 350, COMMON_HEIGHT);
		historyCheck.setSelected(true);
		
		JCheckBox metadataCheck = new JCheckBox(GuiConstants.METADATA_CHECK_TEX1T);
		metadataCheck.setBounds(10, 205, 350, COMMON_HEIGHT);
		metadataCheck.setSelected(true);
		
		JButton startButton = new JButton(GuiConstants.START_BUTTON_START_TEXT);
		startButton.setBounds(10, 250, 120, COMMON_HEIGHT);
		startButton.addActionListener(new StartButtonActionListener(panel));
		
		JButton exitButton = new JButton(GuiConstants.EXIT_BUTTON_TEXT);
		exitButton.setBounds(150, 250, 120, COMMON_HEIGHT);
		exitButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
			
		});
		
		JLabel infoLabel = new JLabel();
		infoLabel.setSize(350, 30);
		infoLabel.setBounds(10, 275, 350, COMMON_HEIGHT);
		
		DefaultCaret caret = (DefaultCaret) loggerField.getCaret();
		caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
		
		JScrollPane scrollPane = new JScrollPane(loggerField);
		scrollPane.setSize(350, 30);
		scrollPane.setBounds(10, 300, 350, 70);
		scrollPane.setAutoscrolls(true);
		
		panel.setLayout(null);
		panel.setBorder(BorderFactory.createEtchedBorder());
		panel.add(urlLabel);
		panel.add(dirLabel);
		panel.add(urlField);
		panel.add(dirField);
		panel.add(parserLabel);
		panel.add(parsersCombo);
		panel.add(historyCheck);
		panel.add(metadataCheck);
		panel.add(startButton);
		panel.add(exitButton);
		panel.add(infoLabel);
		panel.add(scrollPane, BorderLayout.CENTER);
		
		frame.setVisible(true);
	}

}
